﻿//using System;
//using System.Threading;


//namespace Examples.AdvancedProgramming.AsynchronousOperations
//{
//    public class AsyncDemo
//    {
//        // The method to be executed asynchronously.
//        public string TestMethod(int callDuration, out int threadId)
//        {
//            Console.WriteLine("Test method begins.");
//            Thread.Sleep(callDuration); 
//            //throw new Exception("aaaaaaaaaaaaa");
//            threadId = Thread.CurrentThread.ManagedThreadId;
//            return String.Format("My call time was {0}.", callDuration.ToString());
//        }
//    }
//    public delegate string AsyncMethodCaller(int callDuration, out int threadId);


//    public class BeginInvoke相关操作
//    {
////  BeginInvokess
//        static void Main()
//        {

//            int maxthread=0;
//            int comthread=0;
//            ThreadPool.GetMaxThreads(out maxthread, out comthread);

//          bool  b=ThreadPool.SetMaxThreads(5, 5);
//            // The asynchronous method puts the thread id here.
//            int threadId;

//            // Create an instance of the test class.
//            AsyncDemo ad = new AsyncDemo();

//            // Create the delegate.
//            AsyncMethodCaller caller = new AsyncMethodCaller(ad.TestMethod);


//            // Initiate the asychronous call.
//            IAsyncResult result = caller.BeginInvoke(4000,
//                out threadId, null, null);

//            Thread.Sleep(0);
//            Console.WriteLine("Main thread {0} does some work.",
//                Thread.CurrentThread.ManagedThreadId);

//            // Wait for the WaitHandle to become signaled.
//            //result.AsyncWaitHandle.WaitOne();

//            // Perform additional processing here.// Call EndInvoke to retrieve the results.
//            string returnValue = caller.EndInvoke(out threadId, result);

//            // Close the wait handle.
//            result.AsyncWaitHandle.Close();

//            Console.WriteLine("The call executed on thread {0}, with return value \"{1}\".",
//                threadId, returnValue);
//        }
//    }
//    // The delegate must have the same signature as the method// it will call asynchronously.public  delegatestring AsyncMethodCaller(int callDuration, outint threadId);
//}
